<template>
  <div class="container">
    <van-nav-bar fixed left-arrow @click-left="$router.back()" title="小智同学"></van-nav-bar>
    <div class="chat-list" ref="talkArea">
      <div
        class="chat-item"
        :class="[item.name==='xz'?'left':'right']"
        v-for="(item,k) in talks"
        :key="k"
      >
        <van-image fit="cover" round v-if="item.name==='xz'" :src="XZImg" />
        <div class="chat-pao">{{item.msg}}</div>
        <van-image fit="cover" round v-if="item.name==='vip'" :src="userInfo.photo" />
      </div>
    </div>
    <div class="reply-container van-hairline--top">
      <van-field v-model.trim="content" placeholder="说点什么...">
        <van-button size="mini" :loading="isloading" slot="button" @click="send()">提交</van-button>
      </van-field>
    </div>
  </div>
</template>

<script>
import io from 'socket.io-client'
import XZImg from '@/assets/img/xz.png'
import { apiUserInfo } from '@/api/user'
export default {
  name: 'user-chat',
  data () {
    return {
      talks: [],
      socket: null,
      XZImg,
      userInfo: {},
      content: '',
      isloading: false,
      loading: false
    }
  },
  created () {
    this.getUserInfo()
    this.setSocket()
  },
  methods: {
    scrollBottom () {
      this.$nextTick(() => {
        this.$refs.talkArea.scrollTop = this.$refs.talkArea.scrollHeight
      })
    },
    async send () {
      if (!this.content) return false
      this.isloading = true
      const obj = {
        name: 'vip',
        msg: this.content,
        timestamp: Date.now()
      }
      this.talks.push(obj)
      this.content = ''
      this.scrollBottom()
      await this.$sleep(500)
      this.socket.emit('message', obj)
      this.isloading = false
    },
    setSocket () {
      this.socket = io('http://ttapi.research.itcast.cn', {
        query: {
          token: this.$store.state.user.token
        }
      })
      this.socket.on('connect', () => {
        console.log('连接成功')
        this.talks.push({
          msg: '干啥呢outman，咋这长时间不联系了？',
          timestamp: Date.now(),
          name: 'xz'
        })
      })
      this.socket.on('message', data => {
        this.talks.push({ ...data, name: 'xz' })
        this.scrollBottom()
      })
    },
    async getUserInfo () {
      this.userInfo = await apiUserInfo()
    }
  },
  beforeDestory () {
    this.socket.close()
  }
}
</script>

<style scoped lang='less'>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 92px 0 100px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 20px;
      .van-image {
        vertical-align: top;
        width: 80px;
        height: 80px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 80px;
        max-width: 140%;
        min-height: 80px;
        line-height: 76px;
        border: 1px solid #c2d9ea;
        border-radius: 8px;
        position: relative;
        padding: 0 20px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 28px;
        color: #333;
        &::before {
          content: "";
          width: 20px;
          height: 20px;
          position: absolute;
          top: 24px;
          border-top: 1px solid #c2d9ea;
          border-right: 1px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 30px;
    &::before {
      right: -12px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 30px;
    margin-right: 0;
    &::before {
      left: -10px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 88px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
